/*
 * Angry Monsters, Windows Mobile 7 XNA Game FW demo
 * Tero Paananen, tepaanan@gmail.com
 * 2011 Finland
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using System.IO;

namespace WP7Box2d
{

    public class LevelData
    {
        public int m_type;
        public Rectangle m_rectangle;

        public LevelData()
        {
        }

        public void SetData(LevelEditorItem data)
        {
            if (!data.m_placeholder)
            {
                m_type = (int)data.m_type;
                m_rectangle = data.m_rectangle;
            }
        }
    }
    
    
    class LevelStorage
    {
        public List<LevelData> m_dataList = null;
        static public String LEVELNAME = "Level";
        static public String LASTLEVELINDEX = "lastlevelindex";

        public LevelStorage(bool createDefaultLevels)
        {
            if (createDefaultLevels)
                CreateDefaultLevels();
        }


        private void CreateDefaultLevels()
        {
            //DeleteAll();
            if (!LevelExists())
            {
                System.Diagnostics.Debug.WriteLine("Create default levels");
                int freeLevelIndex = NextFreeLevelIndex();
                String newlevelName = String.Empty;
                newlevelName += LevelStorage.LEVELNAME;
                newlevelName += freeLevelIndex.ToString();
                System.Diagnostics.Debug.WriteLine(newlevelName);

                List<LevelEditorItem> list = new List<LevelEditorItem>();
                list.Add(new LevelEditorItem(false,(LevelEditorItem.EditorItemType)2, new Rectangle(357,420,80,40),null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)8, new Rectangle(371,340,44,80), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)4, new Rectangle(466,420,120,40), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)2, new Rectangle(483,380,80,40), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)9, new Rectangle(473,314,109,65), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)3, new Rectangle(636,339,40,120), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)3, new Rectangle(693,340,40,120), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)6, new Rectangle(604,298,160,40), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)7, new Rectangle(651,218,50,80), null));
                StoreLevel(newlevelName, list);
                list.Clear();

                freeLevelIndex = NextFreeLevelIndex();
                newlevelName = String.Empty;
                newlevelName += LevelStorage.LEVELNAME;
                newlevelName += freeLevelIndex.ToString();
                System.Diagnostics.Debug.WriteLine(newlevelName);
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)8, new Rectangle(274, 379, 44, 88), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)3, new Rectangle(344, 340, 40, 120), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)3, new Rectangle(394, 339, 40, 120), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)7, new Rectangle(361, 258, 50, 80), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)0, new Rectangle(456, 416, 40, 40), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)0, new Rectangle(497, 416, 40, 40), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)6, new Rectangle(560, 419, 160, 40), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)3, new Rectangle(618, 298, 40, 120), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)8, new Rectangle(616, 218, 44, 80), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)9, new Rectangle(755, 395, 109, 65), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)9, new Rectangle(892, 379, 50, 80), null));
                StoreLevel(newlevelName, list);
                list.Clear();

                freeLevelIndex = NextFreeLevelIndex();
                newlevelName = String.Empty;
                newlevelName += LevelStorage.LEVELNAME;
                newlevelName += freeLevelIndex.ToString();
                System.Diagnostics.Debug.WriteLine(newlevelName);
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)3, new Rectangle(316, 339, 40, 120), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)4, new Rectangle(280, 298, 120, 40), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)0, new Rectangle(316, 258, 40, 40), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)6, new Rectangle(439, 380, 160, 40), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)8, new Rectangle(373, 376, 44, 80), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)0, new Rectangle(492, 420, 40, 40), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)0, new Rectangle(498, 340, 40, 40), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)8, new Rectangle(614, 380, 40, 80), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)3, new Rectangle(703, 340, 40, 120), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)7, new Rectangle(749, 380, 50, 80), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)3, new Rectangle(807, 339, 40, 120), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)4, new Rectangle(715, 297, 120, 40), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)7, new Rectangle(716, 217, 50, 80), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)7, new Rectangle(778, 214, 50, 80), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)9, new Rectangle(869, 395, 109, 65), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)2, new Rectangle(998, 420, 80, 40), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)1, new Rectangle(1021, 340, 40, 80), null));
                StoreLevel(newlevelName, list);
                list.Clear();


                freeLevelIndex = NextFreeLevelIndex();
                newlevelName = String.Empty;
                newlevelName += LevelStorage.LEVELNAME;
                newlevelName += freeLevelIndex.ToString();
                System.Diagnostics.Debug.WriteLine(newlevelName);
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)3, new Rectangle(404, 340, 40, 120), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)3, new Rectangle(480, 338, 40, 120), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)4, new Rectangle(401, 298, 120, 40), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)1, new Rectangle(438, 218, 40, 80), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)4, new Rectangle(640, 419, 120, 40), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)6, new Rectangle(381, 178, 160, 40), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)9, new Rectangle(651, 354, 109, 65), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)1, new Rectangle(500, 98, 40, 80), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)7, new Rectangle(444, 98, 50, 80), null));
                list.Add(new LevelEditorItem(false, (LevelEditorItem.EditorItemType)1, new Rectangle(389, 98, 40, 80), null));
                StoreLevel(newlevelName, list);
                list.Clear();

            }

        }

        /// <summary>
        /// Next free level index for the level name
        /// </summary>
        /// <returns></returns>
        public int NextFreeLevelIndex()
        {
            int index = -1;

            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            bool ret = settings.TryGetValue<int>(LASTLEVELINDEX, out index);

            if (!ret)
                index = 0;

            index++;

            settings.Remove(LASTLEVELINDEX);
            settings.Add(LASTLEVELINDEX, index);
            settings.Save();

            return index;
        }

        public bool LevelExists()
        {
            int index = -1;
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            bool ret = settings.TryGetValue<int>(LASTLEVELINDEX, out index);
            return ret;
        }

        public int LatestLevelIndex()
        {
            int index = 0;
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            settings.TryGetValue<int>(LASTLEVELINDEX, out index);
            return index;
        }


        /// <summary>
        /// Delete all data from isolated storage
        /// </summary>
        public void DeleteAll()
        {
            using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                String levelName = String.Empty;
                for (int i = 0; i < 10; i++)
                {
                    levelName = LEVELNAME;
                    levelName += i.ToString();
                    System.Diagnostics.Debug.WriteLine("Delete level " + levelName);
                    isolatedStorageFile.DeleteFile(levelName);
                }

            }
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            settings.Remove(LASTLEVELINDEX);
            settings.Save();
            System.Diagnostics.Debug.WriteLine("Delete levelindex ");
        }

        /// <summary>
        /// Store level items into isolated storage
        /// </summary>
        /// <param name="levelName"></param>
        /// <param name="items"></param>
        public void StoreLevel(String levelName, List<LevelEditorItem> items)
        {
            m_dataList = new List<LevelData>();
            //System.Diagnostics.Debug.WriteLine("========== LEVEL ========================");
            for (int i = 0; i < items.Count(); i++)
            {
                if (!items[i].m_placeholder)
                {
                    LevelData data = new LevelData();
                    data.m_rectangle = items[i].m_rectangle;
                    data.m_type = (int)items[i].m_type;

                    //System.Diagnostics.Debug.WriteLine("== ITEM ==");
                    //System.Diagnostics.Debug.WriteLine("rectangle: " + data.m_rectangle);
                    //System.Diagnostics.Debug.WriteLine("type: " + data.m_type);

                    
                    m_dataList.Add(data);
                }
            }

            // Serialize
            using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                isolatedStorageFile.DeleteFile(levelName);
                using (IsolatedStorageFileStream fileStream
                    = isolatedStorageFile.CreateFile(levelName))
                {
                    XmlSerializer xmls = new XmlSerializer(typeof(List<LevelData>));
                    xmls.Serialize(fileStream, m_dataList);
                }
            }
        }

        /// <summary>
        /// Load level item from isolated storage
        /// </summary>
        /// <param name="levelName"></param>
        /// <returns></returns>
        public List<LevelData> LoadLevel(String levelName)
        {
            // Deserialize
            m_dataList = null;
            using (IsolatedStorageFile isolatedStorageFile
                = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isolatedStorageFile.FileExists(levelName))
                {
                    using (IsolatedStorageFileStream fileStream
                        = isolatedStorageFile.OpenFile(levelName, FileMode.Open))
                    {
                        XmlSerializer xmls = new XmlSerializer(typeof(List<LevelData>));
                        m_dataList = (List<LevelData>)xmls.Deserialize(fileStream);
                    }
                }
            }
            return m_dataList;
        }

    

    }
}
